package com.mudalov.rec.dao.model.study;

import com.mudalov.rec.dao.interfaces.IClonable;
import com.mudalov.rec.dao.model.user.Teacher;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Test implements Serializable, IClonable<Test> {

	private static final long serialVersionUID = 6606396239189845674L;

	private Long id;
	
	private String name;
	
	private Subject subject;

    private Teacher owner;
	
	private Set<Question> questions;
	
	public Test() {
		questions = new HashSet<Question>();
	}
	
	public Test(String name, Subject subject, Set<Question> questions) {
		this.name = name;
		this.subject = subject;
		this.questions = questions;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

    private String stringId;

    public String getStringId() {
        return getId() != null ? getId().toString() : "";
    }

    public void setStringId(String stringId) {
        this.stringId = stringId;
    }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Subject getSubject() {
		return subject;
	}

	public void setSubject(Subject subject) {
		this.subject = subject;
	}

	public Set<Question> getQuestions() {
		return questions;
	}

	public void setQuestions(Set<Question> questions) {
		this.questions = questions;
	}
	
	public void addQuestion(Question question) {
		questions.add(question);
	}

    public Teacher getOwner() {
        return owner;
    }

    public void setOwner(Teacher owner) {
        this.owner = owner;
    }

    public String getOwnerName() {
        if (this.owner != null) {
            return owner.getFullName();
        } else {
            return "";
        }
    }

    public void setOwnerName(String name) {
        // nothing to do
    }

    public void setSubjectText(String text) {
        // nothing to do
    }

    public String getSubjectText() {
        if (this.subject != null) {
            return this.subject.getText();
        } else {
            return "";
        }
    }

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((questions == null) ? 0 : questions.hashCode());
		result = prime * result + ((subject == null) ? 0 : subject.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Test))
			return false;
		Test other = (Test) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (questions == null) {
			if (other.questions != null)
				return false;
		} else if (!questions.equals(other.questions))
			return false;
		if (subject == null) {
			if (other.subject != null)
				return false;
		} else if (!subject.equals(other.subject))
			return false;
		return true;
	}

    public Test getClone() {
        Test clone = new Test();
        clone.setId(this.getId());
        clone.setName(this.getName());
        if (this.getSubject() != null) {
            clone.setSubject(this.getSubject().getClone());
        }
        if (this.getOwner() != null) {
            clone.setOwner(this.getOwner().getClone());
        }
        for (Question question : this.questions) {
            clone.getQuestions().add(question.getClone());
        }
        return clone;
    }
	
	
}
